#include "Packet.h"

Packet::Packet()
: m_nReadPos( 0 )
, m_nWritePos( 0 )
{
	m_aStorage.reserve( DEFAULT_SIZE );
}

Packet::Packet( size_t nSize )
: m_nReadPos( 0 )
, m_nWritePos( 0 )
{
	m_aStorage.reserve( nSize );
}

Packet::Packet( const Packet & packet )
: m_aStorage( packet.m_aStorage )
, m_nReadPos( packet.m_nReadPos )
, m_nWritePos( packet.m_nWritePos )
{

}

Packet::~Packet()
{

}

void Packet::Append( const char* pSrc, size_t nCount )
{
	if( !pSrc )
		return ;

	if( m_aStorage.size() < m_nWritePos + nCount )
		m_aStorage.resize( m_nWritePos + nCount );

	memcpy( &m_aStorage[ m_nWritePos ], pSrc, nCount );
	m_nWritePos += nCount;
}

Packet &Packet::operator>>( int& nValue )
{
	nValue = Read<int>();

	return *this;
}

Packet &Packet::operator>>( char& cValue )
{
	cValue = Read< char >();

	return *this;
}

Packet &Packet::operator>>( UCHAR& uValue )
{
	uValue =Read< UCHAR >();

	return *this;
}

Packet &Packet::operator>>( INT64& n64Value )
{
	n64Value = Read< INT64 >();

	return *this;
}

Packet &Packet::operator>>( UINT& uValue )
{
	uValue = Read< UINT >();

	return *this;
}

Packet &Packet::operator>>( UINT64& u64Value )
{
	u64Value = Read< UINT64 >();

	return *this;
}

Packet &Packet::operator>>( float& fValue )
{
	fValue = Read< float >();

	return *this;
}

Packet &Packet::operator>>( double& dValue )
{
	dValue = Read< double >();

	return *this;
}

Packet &Packet::operator>>( short& sValue )
{
	sValue = Read< short >();

	return *this;
}

Packet &Packet::operator>>( USHORT& uValue )
{
	uValue = Read< USHORT >();

	return *this;
}

Packet &Packet::operator>>( ULONG& uLongValue )
{
	uLongValue = Read< ULONG >();

	return *this;
}

Packet &Packet::operator>>( long& lValue )
{
	lValue = Read< long >();

	return *this;
}

Packet &Packet::operator>>( bool& bValue )
{
	bValue = ( Read< char >() ) > 0 ? true : false;

	return *this;
}

Packet &Packet::operator>>( std::string& sValue )
{
	sValue.clear();
	while ( GetReadPos() < GetSize() )
	{
		char c = Read< char >();
		if( 0 == c )
			break;

		sValue += c;
	}

	return *this;
}

void Packet::Read( char* pSrc, int nLength )
{
	if( GetReadPos()  + nLength > GetSize() )
		throw ;

	memcpy( pSrc, &m_aStorage[m_nReadPos], nLength );
	m_nReadPos += nLength;
}

Packet &Packet::operator<<( int nValue )
{
	Append< int >( nValue );

	return *this;
}

Packet &Packet::operator<<( UINT uValue )
{
	Append< UINT >( uValue );

	return *this;
}

Packet &Packet::operator<<( char cValue )
{
	Append< char >( cValue );

	return *this;
}

Packet &Packet::operator<<( UCHAR uValue )
{
	Append< UCHAR >( uValue );

	return *this;
}

Packet &Packet::operator<<( INT64 n64Value )
{
	Append< INT64 >( n64Value );

	return *this;
}

Packet &Packet::operator<<( UINT64 u64Value )
{
	Append< UINT64 >( u64Value );

	return *this;
}

Packet &Packet::operator<<( float fValue )
{
	Append< float >( fValue );

	return *this;
}

Packet &Packet::operator<<( double dValue )
{
	Append< double >( dValue );

	return *this;
}

Packet &Packet::operator<<( short sValue )
{
	Append< short >( sValue );

	return *this;
}

Packet &Packet::operator<<( USHORT uValue )
{
	Append< USHORT >( uValue );

	return *this;
}

Packet &Packet::operator<<( ULONG uLongValue )
{
	Append< ULONG >( uLongValue );

	return *this;
}

Packet &Packet::operator<<( long lValue )
{
	Append< long >( lValue );

	return *this;
}

Packet &Packet::operator<<( bool bValue )
{
	Append< char >( bValue ? 1 : 0 );

	return *this;
}

Packet &Packet::operator<<( const std::string sValue )
{
	Append( ( char const* )sValue.c_str(), sValue.length() );
	Append( (char)0 );

	return *this;
}

Packet &Packet::operator<<( const char* sValue )
{
	Append( ( char const* )sValue, sValue ? strlen( sValue ) : 0 );
	Append( (char)0 );

	return *this;
}

void Packet::Write( const char* sValue, int nLength )
{
	Append( sValue, nLength );
}

char* Packet::GetBuffer()
{
	if( GetSize() <= 0 )
		return NULL;

	return &m_aStorage[ 0 ];
}
